package com.itggm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itggm.Result.ResponseResult;
import com.itggm.pojo.Permission;
import com.itggm.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
  * @Author: ggm
  * @date:  2024/12/23
 * 权限分配
  */
@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {


    @Resource
    private PermissionService permissionService;

    // 权限列表
    @GetMapping("/list")
    public ResponseResult permissionList(){

        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        // 指定字段查询
        lambda.select(Permission::getId,Permission::getPid,Permission::getTitle);
        return  ResponseResult.success().data("treeData",permissionService.list(lambda));
    }
    @GetMapping("/tree")
    public ResponseResult permissionTree(){
        log.info("查询权限列表");
        List<Permission> list = permissionService.list();
        return ResponseResult.success().data("permissionTree",list);
    }
     /**
      * @Author: ggm
      * @date:  2024/12/24
      * 新增或编辑
      */
     @PostMapping("/addOrUpdate")
     public ResponseResult addOrUpdatePermission(@RequestBody Permission permission){
         log.info("新增或编辑{}",permission);
         permissionService.saveOrUpdate(permission);
         return ResponseResult.success();
     }
      /**
       * @Author: ggm
       * @date:  2024/12/24
       * 删除权限
       */
      @DeleteMapping("/delete/{id}")
      public ResponseResult deletePermissionById(@PathVariable Integer id){
          log.info("删除权限{}",id);
          permissionService.removeById(id);
          return ResponseResult.success();

      }
       /**
        * @Author: ggm
        * @date:  2024/12/24
        * 根据权限id查询权限信息
        */
       @GetMapping("/permissionInfo/{id}")
       public ResponseResult getPermissionById(@PathVariable Integer id){
           log.info("根据权限id查询权限信息");
           Permission permission= permissionService.getById(id);
           Integer pid = permission.getPid();
           Permission p = permissionService.getById(pid);
           if (ObjectUtils.isNotNull(p)){
               return ResponseResult.success().data("permission",permission)
                       .data("title",p.getTitle());
           }
           return ResponseResult.success().data("permission",permission);
       }




}
